import 'package:aihu_getx_app/app/modules/chat/widgets/chat_user_list_item_widget.dart';
import 'package:aihu_getx_app/app/modules/commons/commons.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../controllers/chat_controller.dart';

class ChatView extends GetView<ChatController> {
  const ChatView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: Commons.appBar(
        title: controller.title,
        subTitle: controller.appName,
        alwaysShowRefresh: true,
        refresh: controller.onRefresh,
        bottom: PreferredSize(
          preferredSize: const Size.fromHeight(40),
          child: Material(
            color: Get.theme.secondaryHeaderColor.withOpacity(0.6),
            child: TabBar(
              controller: controller.tabController,
              // isScrollable: true,
              indicatorColor: Get.theme.primaryColor,
              indicatorWeight: 3,
              indicatorPadding: const EdgeInsets.all(5),
              indicatorSize: TabBarIndicatorSize.tab,
              indicator: BoxDecoration(
                color: Get.theme.primaryColor,
                borderRadius: BorderRadius.circular(10),
              ),
              labelColor: Get.theme.primaryColor,
              labelStyle: Get.textTheme.titleMedium,
              unselectedLabelStyle: Get.textTheme.titleMedium
                  ?.copyWith(color: Get.theme.secondaryHeaderColor),
              unselectedLabelColor: Get.theme.secondaryHeaderColor,
              tabs: controller.tabList,
              onTap: (index) {
                controller.tabChange(index);
              },
            ),
          ),
        ),
      ),
      body: SizedBox(
        width: Get.width,
        height: Get.height,
        child: Obx(
          () => RefreshIndicator(
            color: Get.theme.primaryColorLight,
            backgroundColor: Get.theme.primaryColor,
            onRefresh: controller.onRefresh,
            child: ListView.builder(
              itemCount: controller.userViewList.length + 1,
              controller: controller.userViewScrollController,
              itemBuilder: (context, index) {
                if (index == controller.userViewList.length) {
                  return controller.buildProgressMoreIndicator();
                }
                return ChatUserListItemWidget(
                  controller.userViewList[index],
                );
              },
            ),
          ),
        ),
      ),
    );
  }
}
